package com.rongxue.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.rongxue.mapper.*;
import com.rongxue.pojo.*;
import com.rongxue.service.UserAccountService;
import com.rongxue.service.utils.P2PMessageUtils;
import com.rongxue.utils.CommonUtils;
import com.rongxue.utils.DateUtil;
import com.rongxue.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

import static java.math.BigDecimal.ROUND_HALF_DOWN;

/**
 * Created by yangweijian on 2018/1/16.
 */
@Service
@Transactional
public class UserAccountServiceImpl implements UserAccountService{

    @Autowired
    private SUserAccountMapper sUserAccountMapper;

    @Autowired
    private SUserWalletRecordsMapper sUserWalletRecordsMapper;

    @Autowired
    private SUserRedenvelopeMapper sUserRedenvelopeMapper;

    @Autowired
    private SProjectMapper sProjectMapper;

    @Autowired
    private SUserBankMapper sUserBankMapper;
    @Autowired
    private SUserMapper sUserMapper;

    @Override
    public SUserAccount selectUserAccountByUid(Long userId) {
        return sUserAccountMapper.selectUserAccountByUserId(userId);
    }

    @Override
    public ObjectResult selectWalletRecordsByMap(XMLBean xmlBean) {
        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> data = new HashMap<String,Object>();
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","token不存在");
        String userId = CommonUtils.decToken(xmlBean.getToken());
        //充值体现记录
        map.put("userId",userId);
        map.put("type",xmlBean.getType());//1 充值 2体现
        List<SUserWalletRecords> list = sUserWalletRecordsMapper.selectWalletRecordsByMap(map);
        //分页
        Page<SUserWalletRecords> page = new Page<>();
        page.setP(xmlBean.getP());
        page.setPageSize(10);
        List<SUserWalletRecords> content = page.getPageContent(list);


        for (SUserWalletRecords sUserWalletRecords :content){
            sUserWalletRecords.setAddTime(sUserWalletRecords.getAddTime().substring(0,sUserWalletRecords.getAddTime().lastIndexOf(".")));
            if(sUserWalletRecords.getType()==2){
                if(sUserWalletRecords.getUser_due_detail_id()>0){
                    sUserWalletRecords.setDescribe("钱包购买产品");
                }
                else {
                    sUserWalletRecords.setDescribe("提现到银行卡");
                }
            }
            else if(sUserWalletRecords.getType()==1){
                if(sUserWalletRecords.getRecharge_no()!=null&!"".equals(sUserWalletRecords.getRecharge_no())){
                    if(sUserWalletRecords.getRecharge_no().contains("R")){
                        sUserWalletRecords.setDescribe("app充值");
                    }
                    else{
                        sUserWalletRecords.setDescribe("官网冲值");
                    }
                }
                else {
                    sUserWalletRecords.setDescribe("还款至零钱包");
                }
            }
        }
        data.put("list",content);
        data.put("count",page.getPageTotal());
        return ObjectResult.build("1","成功",data);
    }

    @Override
    public ObjectResult selectUseRewardsByMap(XMLBean xmlBean) {
        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> data = new HashMap<String,Object>();
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","token不存在");
        //用户可用优惠券
        String userId = CommonUtils.decToken(xmlBean.getToken());
        map.put("userId",userId);
        map.put("money",xmlBean.getMoney());
        map.put("day",xmlBean.getDay());
        List<Rewards> list = sUserRedenvelopeMapper.selectUseRewardsByMap(map);
        //代收利息
        SProject sProject = sProjectMapper.selectProjectByPid(xmlBean.getProject_id());
        BigDecimal interest = new BigDecimal(xmlBean.getMoney()).multiply(sProject.getUser_interest().divide(new BigDecimal(100)))
                .multiply(new BigDecimal(sProject.getDuration())).divide(new BigDecimal(365),2,ROUND_HALF_DOWN);
        //银行卡信息
//        SUserBank userBank = sUserBankMapper.selectUserBankByUserId(userId);
        //可用余额
        SUserAccount userAccount = sUserAccountMapper.selectUserAccountByUid(Long.parseLong(userId));

        data.put("list",list);
        data.put("interest",interest);
//        data.put("bank_name",userBank.getBank_name());
//        data.put("bank_card_no",userBank.getBank_card_no());
//        data.put("bank_code",userBank.getBank_code());
        data.put("account_able",userAccount.getWalletTotle());
        return ObjectResult.build("1","成功",data);
    }

    @Override
    public ObjectResult selectWalletRecords(XMLBean xmlBean) {
        if(xmlBean.getToken()==null||xmlBean.getToken().equals(""))  return ObjectResult.build("0","未登录");
        String userId = CommonUtils.decToken(xmlBean.getToken());
        List<SUserWalletRecords> list = sUserWalletRecordsMapper.selectWalletRecordsByUserId(userId);

        Page<SUserWalletRecords> page = new Page<>();
        page.setP(xmlBean.getP());
        page.setPageSize(10d);
        List<SUserWalletRecords> content = page.getPageContent(list);
        for (SUserWalletRecords s :content){
            //type 0投资 1充值 2提现
            if(s.getType()==null&&s.getValue().compareTo(new BigDecimal(0))>0){
                s.setType(1);
                s.setDescribe("充值成功");
            }
            if(s.getType()==null&&s.getValue().compareTo(new BigDecimal(0))<0){
                s.setType(2);
                s.setDescribe("提现成功");
            }
            if(s.getType()==0){
                s.setDescribe("购买成功");
            }
            s.setAble(s.getValue().toString());
            s.setAddTime(s.getAddTime().substring(0,s.getAddTime().lastIndexOf(".")));
        }
        Map<String,Object> map = new HashMap<>();
        map.put("list",content);
        map.put("connt",page.getPageTotal());

        return ObjectResult.build("1","成功",map);
    }


}